package com.dxt.controller;

import com.dxt.pojo.User;
import com.dxt.service.DeliveryService;
import com.dxt.service.DeliveryServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: DeliveryUpdateController
 * Package: com.dxt.controller
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/3/31 17:11
 * @Version 1.0
 */
@WebServlet("/delivery/updateDelivery")
public class DeliveryUpdateController extends HttpServlet {
    private DeliveryService deliveryService = new DeliveryServiceImpl();
    @Override
    @SneakyThrows
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();

        Map<String, String> map = parameterMap.entrySet().stream().collect(Collectors.toMap(
                Map.Entry::getKey,
                stringEntry -> stringEntry.getValue().length > 0 ? stringEntry.getValue()[0] : ""
        ));
        System.out.println("map = " + map);

        User user = (User)req.getSession().getAttribute("user");
        Integer userId = user.getId();

        deliveryService.updateDeliveryByDeliveryId(map,userId);

        resp.sendRedirect("/delivery/getList");
    }
}
